package it.fuesi.synotools.model.config;

import android.content.SharedPreferences;

import it.fuesi.synotools.activities.SettingsActivity;

/**
 * Created by pascotto on 20/10/14.
 */
public class SynoConfig {

    private static final int PROTOCOL_HTTP = 0;
    private static final int PROTOCOL_HTTPS = 1;

    private String host;
    private int port;
    private String protocol;
    private int timeout;



    private boolean lanEnabled;
    private String lanHost;
    private int lanPort;
    private String lanProtocol;
    private int lanTimeout;

    private String user;
    private String password;

    public static SynoConfig factory(SharedPreferences prefs){
        final SynoConfig config = new SynoConfig();
        config.setHost(prefs.getString(SettingsActivity.PREFERENCE_SYNO_HOST_WAN, null));
        config.setPort(Integer.parseInt(prefs.getString(SettingsActivity.PREFERENCE_SYNO_PORT_WAN, "-1")));
        config.setProtocol(prefs.getString(SettingsActivity.PREFERENCE_SYNO_PROTOCOL_WAN, "http"));
        config.setTimeout(Integer.parseInt(prefs.getString(SettingsActivity.PREFERENCE_SYNO_TIMEOUT_WAN, "30")));

        config.setLanEnabled(prefs.getBoolean(SettingsActivity.PREFERENCE_SYNO_LAN_ENABLED, false));
        config.setLanHost(prefs.getString(SettingsActivity.PREFERENCE_SYNO_HOST_LAN, null));
        config.setLanPort(Integer.parseInt(prefs.getString(SettingsActivity.PREFERENCE_SYNO_PORT_LAN, "-1")));
        config.setLanProtocol(prefs.getString(SettingsActivity.PREFERENCE_SYNO_PROTOCOL_LAN, "http"));
        config.setLanTimeout(Integer.parseInt(prefs.getString(SettingsActivity.PREFERENCE_SYNO_TIMEOUT_LAN, "30")));

        config.setUser(prefs.getString(SettingsActivity.PREFERENCE_SYNO_USER, null));
        config.setPassword(prefs.getString(SettingsActivity.PREFERENCE_SYNO_PASSWORD, null));
        return config;
    }

    public boolean isSet(){
         return (checkWanConfiguration() || checkLanConfiguration()) && checkAccountConfiguration();
    }

    public boolean checkWanConfiguration(){
        return protocol != null && !protocol.isEmpty() && host != null && !host.isEmpty() && port > -1;
    }

    public boolean checkLanConfiguration(){
        return lanEnabled && lanProtocol != null && !lanProtocol.isEmpty() && lanHost != null && !lanHost.isEmpty() && lanPort > -1;
    }

    public boolean checkAccountConfiguration(){
        return user != null && !user.isEmpty() && password != null && !password.isEmpty();
    }

    public String getHost() {
        return host;
    }

    public void setHost(String host) {
        this.host = host;
    }

    public int getPort() {
        return port;
    }

    public void setPort(int port) {
        this.port = port;
    }

    public String getProtocol() {
        return protocol;
    }

    public void setProtocol(String protocol) {
        this.protocol = protocol;
    }

    public String getUser() {
        return user;
    }

    public void setUser(String user) {
        this.user = user;
    }

    public String getPassword() {
        return password;
    }

    public void setPassword(String password) {
        this.password = password;
    }

    public String getProtocolString(){
        /*
        if(protocol == PROTOCOL_HTTP) return "http";
        if(protocol == PROTOCOL_HTTPS) return "https";
        return null;*/
        return this.protocol;
    }

    public String getLanProtocol() {
        return lanProtocol;
    }

    public void setLanProtocol(String lanProtocol) {
        this.lanProtocol = lanProtocol;
    }

    public String getLanHost() {
        return lanHost;
    }

    public void setLanHost(String lanHost) {
        this.lanHost = lanHost;
    }

    public int getLanPort() {
        return lanPort;
    }

    public void setLanPort(int lanPort) {
        this.lanPort = lanPort;
    }

    public boolean isLanEnabled() {
        return lanEnabled;
    }

    public void setLanEnabled(boolean lanEnabled) {
        this.lanEnabled = lanEnabled;
    }

    public int getTimeout() {
        return timeout;
    }

    public void setTimeout(int timeout) {
        this.timeout = timeout;
    }

    public int getLanTimeout() {
        return lanTimeout;
    }

    public void setLanTimeout(int lanTimeout) {
        this.lanTimeout = lanTimeout;
    }


    @Override
    public String toString() {
        return getProtocolString() + "://" + host + ":" + port + " - user:" + user + " password:" + password;
    }
}
